using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using CodeStory.SmartUpload.WebControls;
using CodeStory.SmartUpload.WebControls;

namespace CodeStory.SmartUpload
{
	public sealed class SmartUploadContext
	{
		public const string SMARTID_KEY = "smartcontextid";
		private UploadFileCollection uploadFiles;
		SmartProgress smartProgress;
		public SmartUploadContext() {
			smartProgress = new SmartProgress();
		}

		internal SmartProgress SmartProgress {
			get {
				return smartProgress; }
		}

		public UploadFileCollection UploadFiles {
			get {
				if (uploadFiles == null) {
					uploadFiles = new UploadFileCollection();
				}
				return uploadFiles; }
		}

		public static SmartUploadContext Current {
			get {
				return GetContext(HttpContext.Current);
			}
		}

		public static void CreateContext(HttpContext context) {
			SmartUploadContext c = new SmartUploadContext();
			string id = context.Request.QueryString[SMARTID_KEY];
			if (string.IsNullOrEmpty(id)) {
				return;
			}
			id = string.Format("SMART_{0}",id);
			context.Application[id] = c;
		}

		public static void RemoveContext(HttpContext context) {
			string id = context.Request.QueryString[SMARTID_KEY];
			if (string.IsNullOrEmpty(id)) {
				return;
			}
			id = string.Format("SMART_{0}", id);
			context.Application.Remove(id);
		}
		public static SmartUploadContext GetContext(HttpContext context) {
			string id = context.Request.QueryString[SMARTID_KEY];
			if (string.IsNullOrEmpty(id)) {
				return null;
			}
			id = string.Format("SMART_{0}", id);
			SmartUploadContext c = (SmartUploadContext)context.Application[id];
			return c;
		}
	}
}
